from flask import Blueprint,jsonify
from flask_restful import request
from models import *
cate_blue = Blueprint('cate',__name__)

@cate_blue.route('/getmune')
def getmune():
    data = CateModle.query.filter(CateModle.pid==0).all()
    print(data)
    list = []
    for i in data:
        list.append({
            'id':i.id,
            'cate_name':i.cate_name,
            'pid':i.pid,
            'count':i.count,
        })
    return jsonify({'code':200,'data':list})

@cate_blue.route('/cateall')
def cateall():
    cateid = request.args.get('cateid')
    data = CateModle.query.filter(CateModle.pid==cateid).all()
    list = []
    for i in data:
        list.append({
            'id': i.id,
            'cate_name': i.cate_name,
            'pid': i.pid,
            'count': i.count,
        })
    return jsonify({'code': 200, 'data': list})


@cate_blue.route('/getonecate')
def getonecate():
    data = CateModle.query.filter(CateModle.level==2).all()
    list = []
    for i in data:
        list.append({
            'id': i.id,
            'cate_name': i.cate_name,
            'pid': i.pid,
            'count': i.count,
        })
    return jsonify({'code': 200, 'data': list})

@cate_blue.route('/gettwocate')
def gettwocate():
    onecateid = request.args.get('cateid')
    data = CateModle.query.filter(CateModle.pid==onecateid).all()
    list = []
    for i in data:
        list.append({
            'id': i.id,
            'cate_name': i.cate_name,
            'pid': i.pid,
            'count': i.count,
        })
    return jsonify({'code': 200, 'data': list})


@cate_blue.route('/getthreecate')
def getthreecate():
    onecateid = request.args.get('cateid')
    data = CateModle.query.filter(CateModle.pid==onecateid).all()
    list = []
    for i in data:
        list.append({
            'id': i.id,
            'cate_name': i.cate_name,
            'pid': i.pid,
            'count': i.count,
        })
    return jsonify({'code': 200, 'data': list})



### 前端分类的三级分类
@cate_blue.route('/gettypes')
def gettypes():
    type1 = CateModle.query.all()
    list = []
    mlist = []
    for i in type1:
        if i.pid == 0:
            if i.id not in mlist:
                list.append({'id':i.id,'name':i.cate_name,'son':[]})
                mlist.append([i.id])

    for j in type1:
        for index,p in enumerate(list):
            if j.pid == p['id']:
                list[index]['son'].append({'id':j.id,'name':j.cate_name,'pid':j.pid,'sons':[]})

    for x in type1:
        for index,p in enumerate(list):
            aa = list[index]['son']
            for a,q in enumerate(aa):
                if x.pid == q['id']:
                    aa[a]['sons'].append({'id':x.id,'name':x.cate_name,'pid':x.pid,'sonss':[]})
    print(list)

    for z in type1:
        for index, p in enumerate(list):
            aa = list[index]['son']
            for a, q in enumerate(aa):
                bb = aa[a]['sons']
                print(bb)
                lists = []
                for i in bb:
                    lists.append(i)
                for v,n in enumerate(lists):
                    if z.pid == n['id']:
                        bb[v]['sonss'].append({'id':z.id,'name':z.cate_name,'pid':z.pid})

    return jsonify({'code':200,'list':list})












